package com.helasasa.maxfunds.lineofcredit.cashloan.cameroon.screen.proof.subview

import android.graphics.Color
import android.os.Bundle
import android.view.*
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.fragment.app.DialogFragment
import com.helasasa.maxfunds.lineofcredit.cashloan.R

interface IProofCameroonPhotoPickerListener {
    fun onClick(type: Int)
}

class ProofCameroonPhotoPicker(var onItemClick: IProofCameroonPhotoPickerListener) : DialogFragment() {

    private lateinit var takePictureBg: LinearLayout
    private lateinit var takePictureIcon: ImageView
    private lateinit var takePictureTitle: TextView
    private lateinit var takePictureCheckbox: ImageView

    private lateinit var ablumBg: LinearLayout
    private lateinit var ablumIcon: ImageView
    private lateinit var ablumTitle: TextView
    private lateinit var ablumCheckbox: ImageView

    private lateinit var confirm: TextView

    private var pickType = 0

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE)
        return inflater.inflate(R.layout.view_cameroon_proof_photo_picker, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)

        takePictureBg = view.findViewById(R.id.takePictureBg)
        takePictureIcon = view.findViewById(R.id.takePictureIcon)
        takePictureTitle = view.findViewById(R.id.takePictureTitle)
        takePictureCheckbox = view.findViewById(R.id.takePictureCheckbox)

        ablumBg = view.findViewById(R.id.ablumBg)
        ablumIcon = view.findViewById(R.id.ablumIcon)
        ablumTitle = view.findViewById(R.id.ablumTitle)
        ablumCheckbox = view.findViewById(R.id.ablumCheckbox)

        confirm = view.findViewById(R.id.confirm)

        initializeView()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.BOTTOM
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        }
    }

    private fun initializeView() {

        takePictureIcon.setOnClickListener {
            pickType = 0

            takePictureBg.setBackgroundResource(R.drawable.bg_proof_pick_checked)
            takePictureIcon.setImageResource(R.drawable.pic_pick_take_picture_hl)
            takePictureTitle.setTextColor(Color.parseColor("#5277D6"))
            takePictureCheckbox.setImageResource(R.drawable.pic_pick_hl)

            ablumBg.setBackgroundResource(R.drawable.bg_proof_pick_uncheck)
            ablumIcon.setImageResource(R.drawable.pic_pick_ablum_nor)
            ablumTitle.setTextColor(Color.parseColor("#6D6D6D"))
            ablumCheckbox.setImageResource(R.drawable.pic_pick_nor)
        }

        takePictureTitle.setOnClickListener {
            pickType = 0

            takePictureBg.setBackgroundResource(R.drawable.bg_proof_pick_checked)
            takePictureIcon.setImageResource(R.drawable.pic_pick_take_picture_hl)
            takePictureTitle.setTextColor(Color.parseColor("#5277D6"))
            takePictureCheckbox.setImageResource(R.drawable.pic_pick_hl)

            ablumBg.setBackgroundResource(R.drawable.bg_proof_pick_uncheck)
            ablumIcon.setImageResource(R.drawable.pic_pick_ablum_nor)
            ablumTitle.setTextColor(Color.parseColor("#6D6D6D"))
            ablumCheckbox.setImageResource(R.drawable.pic_pick_nor)
        }

        takePictureCheckbox.setOnClickListener {
            pickType = 0

            takePictureBg.setBackgroundResource(R.drawable.bg_proof_pick_checked)
            takePictureIcon.setImageResource(R.drawable.pic_pick_take_picture_hl)
            takePictureTitle.setTextColor(Color.parseColor("#5277D6"))
            takePictureCheckbox.setImageResource(R.drawable.pic_pick_hl)

            ablumBg.setBackgroundResource(R.drawable.bg_proof_pick_uncheck)
            ablumIcon.setImageResource(R.drawable.pic_pick_ablum_nor)
            ablumTitle.setTextColor(Color.parseColor("#6D6D6D"))
            ablumCheckbox.setImageResource(R.drawable.pic_pick_nor)
        }

        ablumIcon.setOnClickListener {
            pickType = 1

            takePictureBg.setBackgroundResource(R.drawable.bg_proof_pick_uncheck)
            takePictureIcon.setImageResource(R.drawable.pic_pick_take_picture_nor)
            takePictureTitle.setTextColor(Color.parseColor("#6D6D6D"))
            takePictureCheckbox.setImageResource(R.drawable.pic_pick_nor)

            ablumBg.setBackgroundResource(R.drawable.bg_proof_pick_checked)
            ablumIcon.setImageResource(R.drawable.pic_pick_ablum_hl)
            ablumTitle.setTextColor(Color.parseColor("#5277D6"))
            ablumCheckbox.setImageResource(R.drawable.pic_pick_hl)
        }

        ablumTitle.setOnClickListener {
            pickType = 1

            takePictureBg.setBackgroundResource(R.drawable.bg_proof_pick_uncheck)
            takePictureIcon.setImageResource(R.drawable.pic_pick_take_picture_nor)
            takePictureTitle.setTextColor(Color.parseColor("#6D6D6D"))
            takePictureCheckbox.setImageResource(R.drawable.pic_pick_nor)

            ablumBg.setBackgroundResource(R.drawable.bg_proof_pick_checked)
            ablumIcon.setImageResource(R.drawable.pic_pick_ablum_hl)
            ablumTitle.setTextColor(Color.parseColor("#5277D6"))
            ablumCheckbox.setImageResource(R.drawable.pic_pick_hl)
        }

        ablumCheckbox.setOnClickListener {
            pickType = 1

            takePictureBg.setBackgroundResource(R.drawable.bg_proof_pick_uncheck)
            takePictureIcon.setImageResource(R.drawable.pic_pick_take_picture_nor)
            takePictureTitle.setTextColor(Color.parseColor("#6D6D6D"))
            takePictureCheckbox.setImageResource(R.drawable.pic_pick_nor)

            ablumBg.setBackgroundResource(R.drawable.bg_proof_pick_checked)
            ablumIcon.setImageResource(R.drawable.pic_pick_ablum_hl)
            ablumTitle.setTextColor(Color.parseColor("#5277D6"))
            ablumCheckbox.setImageResource(R.drawable.pic_pick_hl)
        }

        confirm.setOnClickListener {
            val curType = pickType
            onItemClick.onClick(curType)
            pickType = 0
            dismiss()
        }
    }
}